package com.openlab.callsystem.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openlab.callsystem.aop.Annotations.Mail;
import com.openlab.callsystem.controller.OneWebSocket;
import com.openlab.callsystem.entity.Caller;
import com.openlab.callsystem.mapper.CallerMapper;
import com.openlab.callsystem.service.CallerService;
import com.openlab.callsystem.utils.QueueBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-11-14
 */
@Service
public class CallerServiceImpl extends ServiceImpl<CallerMapper, Caller> implements CallerService {

    @Autowired
    CallerMapper callerMapper;
    @Override
    @Mail
    public Caller login(String account,String password,Integer windowsId) {
        Caller caller = callerMapper.isTrue(account,password);
        if (caller==null){
            return null;
        }
        return caller;
    }

    @Override
    public List<Caller> queryAllCallers() {
        return callerMapper.queryAll();
    }

    @Override
    public boolean registerCaller(String account, String name, String password, String email) {
        List<Caller> callers = callerMapper.registerCheck(account, name, email);
        if (callers.size()==0){
            callerMapper.insertCaller(new Caller(null,account,name,password,email,LocalDateTime.now()));
            return true;
        }
        else {
            return false;
        }
    }


    @Override
    public void insertCaller(int id, String account, String name, String password, String salt, LocalDateTime date) {
        Caller caller=new Caller(id,account,name,password,salt,date);
        callerMapper.insertCaller(caller);
    }

    @Override
    public void deleteCaller(Integer id) {
        callerMapper.deleteCaller(id) ;
    }

    @Override
    public void updateCaller(int id, String account, String name, String password, String salt, LocalDateTime date) {
        Caller caller=new Caller(id,account,name,password,salt,date);
        callerMapper.updateCaller(caller);
    }

    @Override
    public void repeatCall(Integer number,Integer windowsId) {
        Session session= OneWebSocket.map.get("session");
        try {
            session.getBasicRemote().sendText(String.valueOf(number)+"."+windowsId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
